{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# Adding a background map to plots\n",
    "\n",
    "This example shows how you can add a background basemap to plots created\n",
    "with the geopandas ``.plot()`` method. This makes use of the\n",
    "[contextily](https://github.com/geopandas/contextily) package to retrieve\n",
    "web map tiles from several sources (OpenStreetMap, CartoDB). Also have a\n",
    "look at contextily's \n",
    "[introduction guide](https://contextily.readthedocs.io/en/latest/intro_guide.html#Using-transparent-layers)\n",
    "for possible new features not covered here.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import geopandas\n",
    "import geodatasets\n",
    "import contextily as cx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's use the NYC borough boundary data that is available in geopandas\n",
    "datasets. Plotting this gives the following result:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = geopandas.read_file(geodatasets.get_path(\"nybb\"))\n",
    "ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor=\"k\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Matching coordinate systems \n",
    "\n",
    "\n",
    "Before adding web map tiles to this plot, we first need to ensure the\n",
    "coordinate reference systems (CRS) of the tiles and the data match.\n",
    "Web map tiles are typically provided in\n",
    "[Web Mercator](https://en.wikipedia.org/wiki/Web_Mercator_projection)\n",
    "([EPSG 3857](https://spatialreference.org/ref/epsg/3857/)), so let us first check what\n",
    "CRS our NYC boroughs are in:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.crs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we know the CRS do not match, so we need to choose in which\n",
    "CRS we wish to visualize the data: either the CRS of the tiles,\n",
    "the one of the data, or even a different one.\n",
    "\n",
    "The first option to match CRS is to leverage the `to_crs` method\n",
    "of GeoDataFrames to convert the CRS of our data, here to Web Mercator:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_wm = df.to_crs(epsg=3857)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can then use `add_basemap` function of contextily to easily add a\n",
    "background map to our plot:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "nbsphinx-thumbnail"
    ]
   },
   "outputs": [],
   "source": [
    "ax = df_wm.plot(figsize=(10, 10), alpha=0.5, edgecolor=\"k\")\n",
    "cx.add_basemap(ax)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we want to convert the CRS of the tiles instead, which might be advisable\n",
    "for large datasets, we can use the `crs` keyword argument of `add_basemap`\n",
    "as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor=\"k\")\n",
    "cx.add_basemap(ax, crs=df.crs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This reprojects map tiles to a target CRS which may in some cases cause a\n",
    "loss of sharpness. See \n",
    "[contextily's guide on warping tiles](https://contextily.readthedocs.io/en/latest/warping_guide.html)\n",
    "for more information on the subject."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Controlling the level of detail"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can control the detail of the map tiles using the optional `zoom` keyword\n",
    "(be careful to not specify a too high `zoom` level,\n",
    "as this can result in a large download).:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = df_wm.plot(figsize=(10, 10), alpha=0.5, edgecolor=\"k\")\n",
    "cx.add_basemap(ax, zoom=12)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Choosing a different style"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default, contextily uses the OpenStreetMap HOT style. We can specify a\n",
    "different style using ``cx.providers``:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = df_wm.plot(figsize=(10, 10), alpha=0.5, edgecolor=\"k\")\n",
    "cx.add_basemap(ax, source=cx.providers.CartoDB.Positron)\n",
    "ax.set_axis_off()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding labels as an overlay"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sometimes, when you plot data on a basemap, the data will obscure some important map elements, such as labels,\n",
    "that you would otherwise want to see unobscured. Some map tile providers offer multiple sets of partially\n",
    "transparent tiles to solve this, and `contextily` will do its best to auto-detect these transparent layers\n",
    "and put them on top."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = df_wm.plot(figsize=(10, 10), alpha=0.5, edgecolor=\"k\")\n",
    "cx.add_basemap(ax, source=cx.providers.CartoDB.PositronNoLabels)\n",
    "cx.add_basemap(ax, source=cx.providers.CartoDB.PositronOnlyLabels)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By splitting the layers like this, you can also independently manipulate the level of zoom on each layer,\n",
    "for example to make labels larger while still showing a lot of detail."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = df_wm.plot(figsize=(10, 10), alpha=0.5, edgecolor=\"k\")\n",
    "cx.add_basemap(ax, source=cx.providers.CartoDB.PositronNoLabels, zoom=12)\n",
    "cx.add_basemap(ax, source=cx.providers.CartoDB.PositronOnlyLabels, zoom=10)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
